/**
 * @author Duminda Dharmakeerthi
 */

function printPage(id, page, orientation) {

    if (checkPrinter()) {
        var printContentWrapper = $("#" + id).clone();
        var windowUrl = 'about:blank';
        var uniqueName = new Date();
        var windowName = 'Print' + uniqueName.getTime();
        var printWindow = window.open(windowUrl, windowName, 'left=0,top=0,width=200px,height=200px');

        printWindow.document.write("<head>");
        printWindow.document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\""+$('#baseURL').val()+"/css/style.css\"/>");
        printWindow.document.write("<script language=\"JavaScript\" src=\""+$('#baseURL').val()+"/js/printer.js\"><\/script>");
       // printWindow.document.write("<link rel=\"stylesheet\" type=\"text/css\" media=\"print\" href=\"${pageContext.request.contextPath}/css/print.css\"/></head>");
        printWindow.document.write("<link rel=\"stylesheet\" type=\"text/css\" media=\"print\" href=\""+$('#baseURL').val()+"/css/print.css\"/></head>");
        printWindow.document.write(printContentWrapper.html());
        printWindow.document.close();
        printWindow.focus();
        if (page == 'Legal') {
            printWindow.onload = getLegalPrintout(orientation);
        } else if (page == 'A4') {
            printWindow.onload = getA4Printout(orientation);
        } else {
            // Set default page as A4
            printWindow.onload = getA4Printout(orientation);
        }
        printWindow.close();
    }
}

function getLegalPrintout(o) {
    // Define Legal paper
    jsPrintSetup.setPaperSizeData(5);
    if (o == 'L' || o == 'l') {
        jsPrintSetup.setOption('orientation', jsPrintSetup.kLandscapeOrientation);
    } else {
        jsPrintSetup.setOption('orientation', jsPrintSetup.kPortraitOrientation);
    }

    jsPrintSetup.setGlobalOption('paperWidth', 216);
    jsPrintSetup.setGlobalOption('paperHeight', 356);

    // set page header
    jsPrintSetup.setOption('headerStrLeft', '');
    jsPrintSetup.setOption('headerStrCenter', '');
    jsPrintSetup.setOption('headerStrRight', '');
    // set empty page footer
    jsPrintSetup.setOption('footerStrLeft', '');
    jsPrintSetup.setOption('footerStrCenter', '');
    jsPrintSetup.setOption('footerStrRight', '');

    // set margins.
    jsPrintSetup.setOption('marginTop', 10);
    jsPrintSetup.setOption('marginBottom', 6);
    jsPrintSetup.setOption('marginLeft', 10);
    jsPrintSetup.setOption('marginRight', 8);

    jsPrintSetup.print();
}

function getA4Printout(o) {
    // Define A4 paper
    jsPrintSetup.setPaperSizeData(9);
    if (o == 'L' || o == 'l') {
        jsPrintSetup.setOption('orientation', jsPrintSetup.kLandscapeOrientation);
    } else {
        jsPrintSetup.setOption('orientation', jsPrintSetup.kPortraitOrientation);
    }

    jsPrintSetup.setGlobalOption('paperWidth', 210);
    jsPrintSetup.setGlobalOption('paperHeight', 297);

    // set page header
    jsPrintSetup.setOption('headerStrLeft', '');
    jsPrintSetup.setOption('headerStrCenter', '');
    jsPrintSetup.setOption('headerStrRight', '');
    // set empty page footer
    jsPrintSetup.setOption('footerStrLeft', '');
    jsPrintSetup.setOption('footerStrCenter', '');
    jsPrintSetup.setOption('footerStrRight', '');

    // set margins.
    jsPrintSetup.setOption('marginTop', 15);
    jsPrintSetup.setOption('marginBottom', 6);
    jsPrintSetup.setOption('marginLeft', 15);
    jsPrintSetup.setOption('marginRight', 10);

    jsPrintSetup.print();
}

function checkPrinter() {
    if (typeof(jsPrintSetup) == 'undefined') {
        if (confirm("You don't have printer plugin.\nDo you want to install the Printer Plugin now?")) {
            var xpi = new Object();
            xpi['jsprintsetup'] = 'https://addons.mozilla.org/en-US/firefox/downloads/latest/8966/addon-8966-latest.xpi';
            InstallTrigger.install(xpi);
        }
        return false;
    } else {
        return true;
    }
}
